using UnityEngine;
using kratos;
using rpc;
using System;

public class Network : MonoBehaviour
{
    IRpc rpc_ = null;
    LoginProxy login_prx_ = null;

    class Logger : kratos.ILogger
    {
        public void Debug(string log)
        {
            UnityEngine.Debug.Log(log);
        }

        public void Fail(string log)
        {
            UnityEngine.Debug.LogError(log);
        }

        public void Fatal(string log)
        {
            UnityEngine.Debug.LogError(log);
        }

        public void Info(string log)
        {
            UnityEngine.Debug.Log(log);
        }

        public void Warn(string log)
        {
            UnityEngine.Debug.LogWarning(log);
        }
    }

    // Start is called before the first frame update
    async void Start()
    {
        rpc_ = RpcFactory.create();
        rpc_.Initialize(new Logger());
        rpc_.AutoReconnect = true;
        rpc_.AutoReconnectIntvalMS = 1000;
        rpc_.UseTickAsTimestamp = true;
        bool ret = await rpc_.Connect("127.0.0.1", 6888, 2000);
        Debug.Log($"Connect result:{ret}");
        login_prx_ = Proxy.GetLogin();
    }

    // Update is called once per frame
    void Update()
    {
        rpc_.Update();
    }

    private async void LateUpdate()
    {

        if (login_prx_ != null)
        {
            var id = await login_prx_.login("123", "456");
            Debug.Log($"Login result:{id}");
        }
    }
}
